class ComicsController < ApplicationController
  cache_sweeper :comic_sweeper, :only=>[:create, :update, :destroy]
  before_filter :verify_admin
  
  #TODO: def update_image
  #upload the comic
  #overwrite the images and thumbnails with current  
  #Destroy the current comic
  
  # GET /comics
  # GET /comics.xml
  def index
    @comics = Comic.find(:all, :order=>"rank desc")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @comics }
    end
  end

  # GET /comics/1
  # GET /comics/1.xml
  def show
    redirect_to :controller=>"public", :action=>"webcomic"	
  end

  # GET /comics/new
  # GET /comics/new.xml
  def new
    @comic = Comic.new
	#copy over relevant default values
	if @recent = Comic.most_recent_page
	  #@comic.chapter = @recent.chapter
	  @comic.story = @recent.story
	  #@comic.volume = @recent.volume
	end
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @comic }
    end
  end

  # GET /comics/1/edit
  def edit
    @comic = Comic.find(params[:id])
  end

  # POST /comics
  # POST /comics.xml
  def create
    sleep(5)
	if params[:comic][:two_page] == "1"
	  @comic = Doublecomic.new(params[:comic])
	else
      @comic = Comic.new(params[:comic])
	end

    respond_to do |format|
      if @comic.save
        flash[:notice] = 'Comic was successfully created.'
        format.html { redirect_to comics_url }
        format.xml  { render :xml => @comic, :status => :created, :location => @comic }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @comic.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /comics/1
  # PUT /comics/1.xml
  def update
	if params[:comic][:two_page] == "1"
	  @comic = Doublecomic.find(params[:id])
	else
      @comic = Comic.find(params[:id])
	end	

    respond_to do |format|
      if @comic.update_attributes(params[:comic])
        flash[:notice] = 'Comic was successfully updated.'
        format.html { redirect_to comics_url }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @comic.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /comics/1
  # DELETE /comics/1.xml
  def destroy
    @comic = Comic.find(params[:id])
    @comic.destroy

    respond_to do |format|
      format.html { redirect_to(comics_url) }
      format.xml  { head :ok }
    end
  end

 
protected
  def verify_admin
    unless admin?
	  redirect_to login_path
	  return false
	end
	true
  end  
  

end
